package com.china08.yxyapi.api.manage.tec.office.sch;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DefaultImageUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecOfficeSchSendListController extends YxyApiBaseController {

    @Autowired
    YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/office/cla/send/list", method = RequestMethod.POST)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        String schoolId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();
        Integer status = 0;

        Page<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndUserIdAndDeleteStatus(schoolId, userId, status, pageable);

        List<RespModel> respModelLst = new ArrayList<RespModel>();

        for (YxyNoticeInner yxyNoticeInner : page.getContent()) {
            RespModel respModel = new RespModel();
            respModel.receiverUserNick = yxyNoticeInner.getAllReceiverUserNick(",");
            respModel.readManCount = yxyNoticeInner.getReadManCount(1);
            respModel.readManUserNicknameStr = yxyNoticeInner.getReadManUserNicknameStr(0, "、 ");
            respModel.id = yxyNoticeInner.getId();
            respModel.userId = yxyNoticeInner.getUserId();
            // 判断用户头像
            String avatar = yxyNoticeInner.getAvatar();
            respModel.avatar = DefaultImageUtil.default1(avatar);
            respModel.userNick = yxyNoticeInner.getUserNick();
            respModel.title = yxyNoticeInner.getTitle();
            respModel.content = yxyNoticeInner.getContent();
            respModel.schoolId = yxyNoticeInner.getSchoolId();
            respModel.imageList = yxyNoticeInner.getImage();
            respModel.attachmentName = yxyNoticeInner.getAttachmentName();
            respModel.attachmentPath = yxyNoticeInner.getAttachmentPath();
            respModel.createdDate = DateToolUtils.format(yxyNoticeInner.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD);
            respModelLst.add(respModel);
        }
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());
        return respModelPage;

    }

    public static class ReqModel extends YxyApiPageReqModel {

    }

    public static class RespModel {
        public String id;
        public String userId;
        public String avatar;
        public String userNick;
        public String title;
        public String content;
        public String schoolId;
        public List<String> imageList;
        public String attachmentName;
        public String attachmentPath;
        public String createdDate;
        // 接收用户名称
        public String receiverUserNick;
        // 已收到人数
        public long readManCount;
        // 未收到人员的姓名列表 多个用户昵称使用 、+ 空格 分隔
        public String readManUserNicknameStr;
    }
}
